home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / cutNpaste / MotifClipdemo / clipdemo.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  15.6 KB  |  471 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* 
  19.  * cut/paste example using Motif Clipboard
  20.  */
  21.  
  22.  
  23. #include <X11/Intrinsic.h>
  24. #include <X11/StringDefs.h>
  25. #include <Xm/Xm.h>
  26. #include <Xm/Text.h>
  27. #include <Xm/RowColumn.h>
  28. #include <Xm/MainW.h>
  29. #include <Xm/CascadeB.h>
  30. #include <Xm/BulletinB.h>
  31. #include <Xm/PushB.h>
  32. #include <Xm/CutPaste.h>
  33.  
  34. #define MAXCHR    1024
  35.  
  36. char *selection_buffer1 = NULL; /* malloc'd space to store data whose */
  37.                 /* name was passed to Motif Clipboard */
  38.  
  39. void quitCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data);
  40. void copyByNameCB(Widget w, caddr_t client_data, 
  41.         XmAnyCallbackStruct *call_data);
  42. void copyNamedDataCB(Widget w, int *data_id, int *private, int *reason);
  43. void copyDataCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data);
  44. void pasteDataCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data);
  45. void undoCopyCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data);
  46.  
  47. void inquireCountCB(Widget w, caddr_t client_data, 
  48.         XmAnyCallbackStruct *call_data);
  49. void inquireFormatCB(Widget w, caddr_t client_data, 
  50.         XmAnyCallbackStruct *call_data);
  51. void inquireLengthCB(Widget w, caddr_t client_data, 
  52.         XmAnyCallbackStruct *call_data);
  53. void inquirePendingItemsCB(Widget w, caddr_t client_data, 
  54.         XmAnyCallbackStruct *call_data);
  55.  
  56. /* ======================== */
  57. void
  58. main(int argc, char **argv)
  59. {
  60.    Arg args[20];
  61.    Cardinal argcount;
  62.    Widget main_widget, main_window, menu_bar, new_menu, text_widget;
  63.    Widget cascade1, cascade2, cascade3, pulldown1, pulldown2, pulldown3;
  64.    Widget pb1, pb2, pb3, pb4, pb5, pb6, pb7, pb8, pb9;
  65.  
  66.    main_widget = XtInitialize(argv[0], "ClipTest", NULL, 0, &argc, argv);
  67.  
  68.    main_window = XmCreateMainWindow(main_widget, "Main", NULL, 0);
  69.    XtManageChild(main_window);
  70.  
  71.    menu_bar = XmCreateMenuBar(main_window, "Menubar", NULL, 0);
  72.    XtManageChild(menu_bar);
  73.  
  74.    argcount = 0;
  75.    XtSetArg(args[argcount], XmNeditable, True); argcount++;
  76.    XtSetArg(args[argcount], XmNeditMode, XmMULTI_LINE_EDIT); argcount++;
  77.    XtSetArg(args[argcount], XmNcolumns, 60); argcount++;
  78.    XtSetArg(args[argcount], XmNrows, 15); argcount++;
  79.    XtSetArg(args[argcount], XmNx, 15); argcount++;
  80.    XtSetArg(args[argcount], XmNy, 15); argcount++;
  81.    text_widget = XmCreateText(main_window, "TextWidget", args, argcount);
  82.    XtManageChild(text_widget);
  83.  
  84.    XmMainWindowSetAreas(main_window, menu_bar, NULL, NULL, NULL, text_widget);
  85.  
  86.    /* create the menus */
  87.    pulldown1 = XmCreatePulldownMenu(menu_bar, "pulldown1", NULL, 0);
  88.    pulldown2 = XmCreatePulldownMenu(menu_bar, "pulldown2", NULL, 0);
  89.    pulldown3 = XmCreatePulldownMenu(menu_bar, "pulldown3", NULL, 0);
  90.    XtSetArg(args[0], XmNsubMenuId, pulldown1);
  91.    cascade1 = XmCreateCascadeButton(menu_bar, "File", args, 1);
  92.    XtSetArg(args[0], XmNsubMenuId, pulldown2);
  93.    cascade2 = XmCreateCascadeButton(menu_bar, "Edit", args, 1);
  94.    XtSetArg(args[0], XmNsubMenuId, pulldown3);
  95.    cascade3 = XmCreateCascadeButton(menu_bar, "Inquire", args, 1);
  96.    XtManageChild(cascade1); XtManageChild(cascade2); XtManageChild(cascade3);
  97.  
  98.    /* put buttons in the menus */
  99.    pb1 = XmCreatePushButton(pulldown1, "Quit", NULL, 0);
  100.    XtAddCallback(pb1, XmNactivateCallback, quitCB, text_widget);
  101.    XtManageChild(pb1); 
  102.  
  103.    pb8 = XmCreatePushButton(pulldown2, "Copy Name", NULL, 0);
  104.    XtAddCallback(pb8, XmNactivateCallback, copyByNameCB, text_widget);
  105.    pb4 = XmCreatePushButton(pulldown2, "Copy Data", NULL, 0);
  106.    XtAddCallback(pb4, XmNactivateCallback, copyDataCB, text_widget);
  107.    pb5 = XmCreatePushButton(pulldown2, "Paste", NULL, 0);
  108.    XtAddCallback(pb5, XmNactivateCallback, pasteDataCB, text_widget);
  109.    pb9 = XmCreatePushButton(pulldown2, "Undo Copy", NULL, 0);
  110.    XtAddCallback(pb9, XmNactivateCallback, undoCopyCB, text_widget);
  111.    XtManageChild(pb4); XtManageChild(pb5); 
  112.    XtManageChild(pb8); XtManageChild(pb9);
  113.  
  114.    pb2 = XmCreatePushButton(pulldown3, "Count", NULL, 0);
  115.    XtAddCallback(pb2, XmNactivateCallback, inquireCountCB, text_widget);
  116.    pb3 = XmCreatePushButton(pulldown3, "Format", NULL, 0);
  117.    XtAddCallback(pb3, XmNactivateCallback, inquireFormatCB, text_widget);
  118.    pb6 = XmCreatePushButton(pulldown3, "Length", NULL, 0);
  119.    XtAddCallback(pb6, XmNactivateCallback, inquireLengthCB, text_widget);
  120.    pb7 = XmCreatePushButton(pulldown3, "PendingItems", NULL, 0);
  121.    XtAddCallback(pb7, XmNactivateCallback, inquirePendingItemsCB, text_widget);
  122.    XtManageChild(pb2); XtManageChild(pb3);
  123.    XtManageChild(pb6); XtManageChild(pb7);
  124.  
  125.  
  126.    XtRealizeWidget(main_widget);
  127.    XtMainLoop();
  128.  
  129. }
  130.  
  131. /*
  132.  ***** end of main, start of callbacks 
  133.  */
  134.  
  135. void copyByNameCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
  136. {
  137.    char *selection;
  138.    long item_id, data_id;
  139.    XButtonEvent *button_event = (XButtonEvent *) call_data->event;
  140.    Widget w_text = (Widget) client_data;
  141.    static int private = 1;
  142.  
  143.    if((selection = XmTextGetSelection(w_text)) == NULL) return ;
  144.    if((void *)selection_buffer1 != NULL) free((void *)selection_buffer1);
  145.    selection_buffer1 = (char *)malloc(strlen(selection)+1);
  146.    strcpy(selection_buffer1, selection);
  147.  
  148.    while(XmClipboardStartCopy(XtDisplay(w), XtWindow(w_text), "ClipDemo",
  149.         button_event->time, w_text, copyNamedDataCB, &item_id) 
  150.             != ClipboardSuccess) ;
  151.  
  152.    while(XmClipboardCopy(XtDisplay(w), XtWindow(w_text), item_id,
  153.         "STRING", NULL, strlen(selection),
  154.         private++, &data_id) != ClipboardSuccess) ;
  155.  
  156.    while(XmClipboardEndCopy(XtDisplay(w), XtWindow(w_text), item_id) 
  157.             != ClipboardSuccess) ;
  158.  
  159. printf("%d >>%s<<\n",strlen(selection_buffer1),selection_buffer1);
  160.    XtFree(selection);
  161. }
  162.  
  163. void copyNamedDataCB(Widget w, int *data_id, int *private, int *reason)
  164. {
  165.    Widget w_text = w;
  166.  
  167.    printf("copyNamedDataCB: data id %d, private %d\n ", *data_id, *private);
  168.    printf("copyNamedDataCB: data = %s\n", selection_buffer1);
  169.    if(*reason == XmCR_CLIPBOARD_DATA_DELETE)
  170.    {
  171.       printf("allowed to delete named data since its no longer needed. \n");
  172.       free(selection_buffer1);
  173.       return ;
  174.    }
  175.    printf("copying named data to the Motif Clipboard. \n");
  176. printf("%d >>%s<<\n",strlen(selection_buffer1),selection_buffer1);
  177.    while(XmClipboardCopyByName(XtDisplay(w), XtWindow(w_text), *data_id,
  178.         selection_buffer1, strlen(selection_buffer1), 
  179.         *private ) == ClipboardLocked ) ;
  180.    printf("copyNamedDataCB: operation completed. \n");
  181.    return ;
  182. }
  183.  
  184. void copyDataCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
  185. {
  186.    char *selection;
  187.    long item_id, data_id;
  188.    XButtonEvent *button_event = (XButtonEvent *) call_data->event;
  189.    Widget w_text = (Widget) client_data;
  190.  
  191.    if((selection = XmTextGetSelection(w_text)) == NULL) return ;
  192.  
  193.    while(XmClipboardStartCopy(XtDisplay(w), XtWindow(w_text), "ClipDemo",
  194.         button_event->time, w_text, NULL, &item_id) != ClipboardSuccess) ;
  195.  
  196.    while(XmClipboardCopy(XtDisplay(w), XtWindow(w_text), item_id,
  197.         "STRING", selection, strlen(selection),
  198.         0, &data_id) != ClipboardSuccess) ;
  199.  
  200.    while(XmClipboardEndCopy(XtDisplay(w), XtWindow(w_text), item_id) 
  201.             != ClipboardSuccess) ;
  202.  
  203.    XtFree(selection);
  204. }
  205.  
  206. void pasteDataCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
  207. {
  208.    int id, insertion_point, stat, copy_done = FALSE, numchars;
  209.    Widget w_text = (Widget) client_data;
  210.    char buf[MAXCHR];
  211.    XButtonEvent *button_event = (XButtonEvent *) call_data->event;
  212.  
  213.    printf("pasteDataCB: about to start \n");
  214.    while(XmClipboardStartRetrieve(XtDisplay(w), XtWindow(w_text), 
  215.         button_event->time) == ClipboardLocked ) ;
  216.  
  217.    /* put any required clipboard inquire function calls HERE!! */
  218.  
  219.    while(!copy_done)
  220.    {
  221.       while((stat = XmClipboardRetrieve(XtDisplay(w), XtWindow(w_text), 
  222.             "STRING", buf, MAXCHR, &numchars, &id)) 
  223.                 == ClipboardLocked) ;
  224.       switch(stat)
  225.       {
  226.      case ClipboardNoData: 
  227.         printf("pasteDataCB: no data to paste from clipboard \n");
  228.         copy_done = TRUE;
  229.         break;
  230.  
  231.      case ClipboardTruncate: 
  232.         printf(" more data remains to be copied from Clipboard \n");
  233.         buf[numchars] = '\0';
  234.             printf("pasteDataCB: buf = %s\n", buf);
  235.         insertion_point = XmTextGetInsertionPosition(w_text);
  236.         XmTextReplace(w_text, insertion_point, insertion_point, buf);
  237.         copy_done = FALSE;
  238.         break;
  239.  
  240.      case ClipboardSuccess:
  241.         printf("pasteDataCB: got all available data from clipboard \n");
  242.         buf[numchars] = '\0';
  243.             printf("pasteDataCB: buf = %s\n", buf);
  244.         insertion_point = XmTextGetInsertionPosition(w_text);
  245.         XmTextReplace(w_text, insertion_point, insertion_point, buf);
  246.         copy_done = TRUE;
  247.         break;
  248.       }
  249.    }
  250.    while(XmClipboardEndRetrieve(XtDisplay(w), XtWindow(w_text)) 
  251.         == ClipboardLocked ) ;
  252.    printf("pasteDataCB: operation completed \n");
  253.    return ;
  254. }
  255.  
  256. void undoCopyCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
  257. {
  258.    Widget w_text = (Widget) client_data;
  259.  
  260.    printf("undoing the last copy by this app to the Motif Clipboard. \n");
  261.    while(XmClipboardUndoCopy(XtDisplay(w), XtWindow(w_text))
  262.             == ClipboardLocked ) ;
  263.    return ;
  264. }
  265.  
  266. void quitCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
  267. {
  268.     
  269.    int stat, flag = True;
  270.    Widget w_text = (Widget) client_data;
  271.    int index, i;
  272.    unsigned long buffer_len = 101;
  273.    unsigned long copied_len;
  274.    char format_name_buf[101], str[101];
  275.    unsigned long icount;
  276.    XmClipboardPendingList ilist;
  277.  
  278.     if (selection_buffer1 != NULL)
  279.     printf("%d >>%s<<\n",strlen(selection_buffer1),selection_buffer1);
  280.    while(( stat = XmClipboardInquirePendingItems(XtDisplay(w), 
  281.     XtWindow(w_text), "STRING", &ilist, &icount )) 
  282.         == ClipboardLocked ) ;
  283.    if(icount == 0)
  284.         printf("There are no pending items for this format.\n");
  285.    else {
  286.         for( i = 0; i < icount; i++) {
  287.            printf("\t pair # %d: data id %d | private id %d \n", 
  288.                 i, (ilist+i)->DataId, (ilist+i)->PrivateId);
  289.            while(XmClipboardCopyByName(XtDisplay(w), XtWindow(w_text), 
  290.                 (ilist+i)->DataId, selection_buffer1, 
  291.                 strlen(selection_buffer1), 
  292.                 (ilist+i)->PrivateId ) == ClipboardLocked ) ;
  293.             printf("%d >>%s<<\n",strlen(selection_buffer1),selection_buffer1);
  294.         }
  295.         XtFree(ilist);
  296.      }
  297.    XtCloseDisplay(XtDisplay(w));
  298.    exit(0);
  299. }
  300.  
  301. void inquireCountCB(Widget w, caddr_t client_data, 
  302.         XmAnyCallbackStruct *call_data)
  303. {
  304.    int stat;
  305.    Widget w_text = (Widget) client_data;
  306.    int count, max_format_name_length;
  307.  
  308.    while(( stat = XmClipboardInquireCount(XtDisplay(w), XtWindow(w_text), 
  309.         &count, &max_format_name_length )) == ClipboardLocked ) ;
  310.    if(stat == ClipboardNoData) 
  311.       printf("no data on Motif Clipboard\n");
  312.    else 
  313.    {
  314.       printf("\nMotif Clipboard has %d data items\n", count);
  315.       printf(" and the max length of the format names is %d\n",
  316.         max_format_name_length);
  317.    }
  318. }
  319.  
  320. void inquireFormatCB(Widget w, caddr_t client_data, 
  321.         XmAnyCallbackStruct *call_data)
  322. {
  323.    int stat, flag = True;
  324.    Widget w_text = (Widget) client_data;
  325.    int index;
  326.    unsigned long buffer_len = 101;
  327.    unsigned long copied_len;
  328.    char format_name_buf[101], str[101];
  329.  
  330.    for(index = 1; index < 11; index++)
  331.    {
  332.       while(( stat = XmClipboardInquireFormat(XtDisplay(w), XtWindow(w_text), 
  333.            index, format_name_buf, buffer_len, &copied_len)) 
  334.                == ClipboardLocked ) ;
  335.       if(stat == ClipboardTruncate) 
  336.          printf("format name buffer not long enough to hold data \n");
  337.       else if (stat == ClipboardNoData) break;
  338.       else if(copied_len == 0) 
  339.       {
  340.      if(index == 0) printf("Motif Clipboard has no data \n");
  341.      /* else printf("Motif Clipboard has no additional data formats\n"); */
  342.      break;
  343.       }
  344.       else 
  345.       {
  346.      if (flag)
  347.      {
  348.         flag = False;
  349.         printf("\n*** Motif Clipboard formats *** \n");
  350.      }
  351.          printf("index %d:", index);
  352.      strncpy(str, format_name_buf, copied_len);
  353.      str[copied_len] = '\0';
  354.          printf("format name len %d, ", copied_len);
  355.          printf("format name %s \n", str);
  356.       }
  357.    }
  358.  
  359.    if(index == 0) printf("no data on Motif Clipboard\n");
  360. }
  361.  
  362. void inquireLengthCB(Widget w, caddr_t client_data, 
  363.         XmAnyCallbackStruct *call_data)
  364. {
  365.    int stat, flag = True;
  366.    Widget w_text = (Widget) client_data;
  367.    int index;
  368.    unsigned long buffer_len = 101;
  369.    unsigned long copied_len;
  370.    unsigned long length;
  371.    char format_name_buf[101], str[101];
  372.  
  373.    for(index = 1; index < 11; index++)
  374.    {
  375.       while(( stat = XmClipboardInquireFormat(XtDisplay(w), XtWindow(w_text), 
  376.            index, format_name_buf, buffer_len, &copied_len)) 
  377.                == ClipboardLocked ) ;
  378.       if(stat == ClipboardTruncate) 
  379.          printf("format name buffer not long enough to hold data \n");
  380.       else if (stat == ClipboardNoData) break;
  381.       else if(copied_len == 0) 
  382.       {
  383.      if(index == 0) printf("Motif Clipboard has no data \n");
  384.      /* else printf("Motif Clipboard has no additional data formats\n"); */
  385.      break;
  386.       }
  387.       else 
  388.       {
  389.      if (flag)
  390.      {
  391.         flag = False;
  392.         printf("\n*** Motif Clipboard data lengths by format *** \n");
  393.      }
  394.          printf("index %d:", index);
  395.      strncpy(str, format_name_buf, copied_len);
  396.      str[copied_len] = '\0';
  397.          printf("format name len %d, ", copied_len);
  398.          printf("format name %s, ", str);
  399.  
  400.          while(( stat = XmClipboardInquireLength(XtDisplay(w), 
  401.             XtWindow(w_text), str, &length)) == ClipboardLocked ) ;
  402.      if(stat == ClipboardNoData)
  403.         printf("Data is Not Available\n");
  404.          else printf("data length %ld \n", length);
  405.       }
  406.    }
  407.  
  408.    if(index == 0) printf("no data on Motif Clipboard\n");
  409. }
  410.  
  411. void inquirePendingItemsCB(Widget w, caddr_t client_data, 
  412.         XmAnyCallbackStruct *call_data)
  413. {
  414.    int stat, flag = True;
  415.    Widget w_text = (Widget) client_data;
  416.    int index, i;
  417.    unsigned long buffer_len = 101;
  418.    unsigned long copied_len;
  419.    char format_name_buf[101], str[101];
  420.    unsigned long icount;
  421.    XmClipboardPendingList ilist;
  422.  
  423.    for(index = 1; index < 11; index++)
  424.    {
  425.       while(( stat = XmClipboardInquireFormat(XtDisplay(w), XtWindow(w_text), 
  426.            index, format_name_buf, buffer_len, &copied_len)) 
  427.                == ClipboardLocked ) ;
  428.       if(stat == ClipboardTruncate) 
  429.          printf("format name buffer not long enough to hold data \n");
  430.       else if (stat == ClipboardNoData) break;
  431.       else if(copied_len == 0) 
  432.       {
  433.      if(index == 0) printf("Motif Clipboard has no data \n");
  434.      /* else printf("Motif Clipboard has no additional data formats\n"); */
  435.      break;
  436.       }
  437.       else 
  438.       {
  439.      if (flag)
  440.      {
  441.         flag = False;
  442.         printf("\n*** Motif Clipboard pending items, by format *** \n");
  443.      }
  444.          printf("index %d:", index);
  445.      strncpy(str, format_name_buf, copied_len);
  446.      str[copied_len] = '\0';
  447.          printf("format name len %d, ", copied_len);
  448.          printf("format name %s:\n ", str);
  449.  
  450.          while(( stat = XmClipboardInquirePendingItems(XtDisplay(w), 
  451.             XtWindow(w_text), str, &ilist, &icount )) 
  452.                 == ClipboardLocked ) ;
  453.      if(icount == 0)
  454.         printf("There are no pending items for this format.\n");
  455.          else 
  456.      {
  457.         for( i = 0; i < icount; i++)
  458.         {
  459.            printf("\t pair # %d: data id %d | private id %d \n", 
  460.             i, ilist->DataId, ilist->PrivateId);
  461.         }
  462.         XtFree(ilist);
  463.      }
  464.       }
  465.    }
  466.  
  467.    if(index == 0) printf("no data on Motif Clipboard\n");
  468. }
  469.  
  470. /* end of xmnotes.c */
  471.